package com.yict.csms.system.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yict.common.entity.PageEntity;
import com.yict.common.service.impl.CommonService;
import com.yict.common.util.DateUtil;
import com.yict.csms.system.dao.impl.ExchangeRateDaoImpl;
import com.yict.csms.system.entity.ExchangeRate;
import com.yict.csms.system.service.IExchangeRateService;

/**
 * 
 * 
 * @author Patrick.Deng
 * 
 */
@Service
public class ExchangeRateServiceImpl extends CommonService<ExchangeRate, Long> implements
		IExchangeRateService {
	@Autowired
	private ExchangeRateDaoImpl exchangeRateDaoImpl;

	public List<ExchangeRate> search(Map<String, Object> queryMap, PageEntity page) {
		StringBuilder queryString = new StringBuilder();
		String field = " order by c.createDate desc";
		pageEntity = new PageEntity();
		queryString.append("from ExchangeRate as c where 1=1");
		if (queryMap != null && queryMap.size() > 0) {
			if (queryMap.get("beginDate") != null && !"".equals(queryMap.get("beginDate"))&&queryMap.get("endDate")==null) {
				queryMap.put("beginDate", DateUtil.StringToDate((String)queryMap.get("beginDate"), 1));
				queryString.append(" and c.endDate >= :beginDate");
			}else if (queryMap.get("endDate") != null && !"".equals(queryMap.get("endDate"))&&queryMap.get("beginDate") == null) {
				queryMap.put("endDate", DateUtil.StringToDate((String)queryMap.get("endDate"), 1));
				queryString.append(" and c.endDate >= :endDate");
			}else if(queryMap.get("beginDate") != null && queryMap.get("endDate") != null){
				queryMap.put("beginDate", DateUtil.StringToDate((String)queryMap.get("beginDate"), 1));
				queryMap.put("endDate", DateUtil.StringToDate((String)queryMap.get("endDate"), 1));
				queryString.append(" and ((c.beginDate <= :beginDate and c.endDate >= :beginDate) or (c.beginDate <= :endDate and c.endDate >= :endDate) or (c.beginDate >= :beginDate and c.endDate <= :endDate))");
			}
		}
		if (page.getField() != null&&!"".equals(page.getField()) &&page.getOrder()!= null && !"".equals(page.getOrder())) {
			field = " order by c." + page.getField() + page.getOrder();
		}
		queryString.append(field);

		List<ExchangeRate> list = exchangeRateDaoImpl.search(queryString.toString(), queryMap,
				page);
		pageEntity.setTotalRecord(exchangeRateDaoImpl.getPageEntity().getTotalRecord());
		return list;
	}

	public Date findByCurrency(String fromCurrency) {
		return exchangeRateDaoImpl.findByCurrency(fromCurrency);
	}

	/**
	 * 查找是否存在相同的币种组合,且时间范围有重叠
	 * @param exchangeRate
	 * @param isSave
	 * @return true-存在
	 */
	public ExchangeRate check(ExchangeRate exchangeRate, boolean isSave) {
		StringBuilder queryString = new StringBuilder();
		Map<String, Object> queryMap = new HashMap<String, Object>();
		queryString.append("from ExchangeRate as c where 1=1 and ( c.fromCurrency.dictid =:fromCurrency or c.fromCurrency.dictid =:toCurrency )" +
				" and ( c.toCurrency.dictid =:toCurrency or c.toCurrency.dictid =:fromCurrency )" +
				"and ((c.beginDate >= :beginDate and c.beginDate <=:enddate) or (c.endDate >= :beginDate and c.endDate <=:enddate)) ");
		queryMap.put("fromCurrency", exchangeRate.getFromCurrency().getDictid());
		queryMap.put("toCurrency", exchangeRate.getToCurrency().getDictid());
		queryMap.put("beginDate", exchangeRate.getBeginDate());
		queryMap.put("enddate", exchangeRate.getEndDate());
		
		if(isSave) {
			queryString.append(" order by c.createPerson");
		} else {
			queryString.append(" and c.exRateId not in (:exRateId) order by c.createPerson");
			queryMap.put("exRateId", exchangeRate.getExRateId());
		}
		List<ExchangeRate> list =  exchangeRateDaoImpl.search(queryString.toString(),queryMap,null);
		if (list == null || list.size() <= 0) {
			return null;
		}
		return list.get(0);
	}
	
	public ExchangeRate findExchangeRate(Date cdate,String fromCrrency,String toCrrency){
		String hql = "from ExchangeRate t where t.beginDate <= :cdate and t.endDate >= :cdate " +
				"and t.fromCurrency.dictid = :fromCrrency and t.toCurrency.dictid = :toCrrency";
		Map<String,Object> map = new HashMap<String, Object>();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(cdate);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		//calendar.set(Calendar.MILLISECOND, 0);
		cdate = calendar.getTime();
		map.put("cdate", cdate);
		map.put("fromCrrency", fromCrrency);
		map.put("toCrrency", toCrrency);
		List<ExchangeRate> list = exchangeRateDaoImpl.search(hql, map, null);
		ExchangeRate rate = null;
		if(list.size() > 0) rate = list.get(0);
		return rate;
	}

	/**
	 * 查找当前记录是否该币种按开始日期排序的最后的记录
	 * @param exchangeRate
	 * @return true-存在
	 */
	public boolean check(ExchangeRate exchangeRate) {
		StringBuilder queryString = new StringBuilder();
		Map<String, Object> queryMap = new HashMap<String, Object>();
		queryString.append("from ExchangeRate as c where 1=1 and c.fromCurrency.dictid =:fromCurrency and c.beginDate >:beginDate ");
		queryMap.put("fromCurrency", exchangeRate.getFromCurrency().getDictid());
		queryMap.put("beginDate", exchangeRate.getBeginDate());
		queryString.append(" order by c.beginDate");
		List<ExchangeRate> list =  exchangeRateDaoImpl.search(queryString.toString(),queryMap,null);
		if (list == null || list.size() <= 0) {
			return false;
		}
		return true;
	}
	
	public ExchangeRate findById(Long id) {
		return exchangeRateDaoImpl.findbyId(id);
	}
}
